package com.amado.course42Socket.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Administrator on 2018/8/7.
 */
public class HelloSocketService {
    private int port = 8888;
    private ServerSocket serverSocket;

    public HelloSocketService() throws Exception{
        serverSocket = new ServerSocket(port);
        System.out.println("服务器启动!");
    }
    public void service(){
        while(true){
            Socket socket = null;
            try {
            	//循环等待客户端接入
                socket = serverSocket.accept();
                System.out.println("New connection accepted "+
                        socket.getInetAddress()+":"+socket.getPort());
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(socket!=null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws Exception{
        HelloSocketService server = new HelloSocketService();
        server.service();
    }
    
    //name   //getname
    //getName'
    //name  n  -> Name +get getName
    //name.substring(0,1) n N   ame
}
